/*
 *  Confidential and Proprietary                                                                
 Copyright 2008 By                                                                                     
 SGAI & Hewlett-Packard Development Company, L.P. 	              
 All Rights Reserved                                                                                  

 Project Name : SGAI  MES                                                                                                                                       
 Class Name   : LongConverter.java    
 Package      : com.hp.message.vo.converter   
 */
package com.erdos.message.vo.converter;

import org.apache.commons.lang.StringUtils;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

/**
 * ---------------------------------------------------------------------------------
                                                                
 @version     $Id: LongConverter.java 9110 2012-03-08 02:08:48Z hubin $                                                          
 @author sunf
 @since  2008-11-11 
 */
public class LongConverter implements Converter {

	@SuppressWarnings("unchecked")
	public boolean canConvert(Class type) {
		return Long.class.equals(type) || long.class.equals(type);
	}

	public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
		if(source==null || source.equals(Long.MIN_VALUE)) {
			writer.setValue("");
		} else {
			writer.setValue(String.valueOf(source));
		}		
	}

	public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
		String value=reader.getValue();
		if(StringUtils.isNotBlank(value)) {
			return Long.valueOf(value.trim(), 10);
		} else {
			return null;
		}		
	}

}
